package Model;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import Information.Enterprise;

public class EnterpriseDAO{
	
	private int ENO;
	private String EName;
	private String EUser;
	private String EPassword;
	private String EAddress;
	private String EEmail;
	
	public EnterpriseDAO()
	{
		this.EName = "";
		this.EUser = "";
		this.EPassword = "";
		this.EAddress = "";
		this.EEmail = "";
	}
	
	public boolean AddNewEnterprise(String EName,String EUser,String EPassword,String EAddress, String EEmail) throws SQLException, ClassNotFoundException
	{
		String sql ="insert into Enterprise (EName,EUser,EPassword,EAddress,EEmail) values('"+EName+"','"+EUser+"','"+EPassword+"','"+EAddress+"','"+EEmail+"')";
		Connection con = null;
		Statement stmt = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		if(stmt.executeUpdate(sql)==1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public ArrayList<Enterprise> SeeEnterprise(String EUser) throws SQLException, ClassNotFoundException
	{
		ArrayList<Enterprise> arrayList = null;
		Enterprise enterprise = new Enterprise();
		String sql ="select * from Enterprise where EUser ='"+EUser+"'";
		Connection con = null;
		Statement stmt = null;
		ResultSet result = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		result = stmt.executeQuery(sql);
		ENO = result.getInt("ENO");
		EUser = result.getString("EUser");
		EName = result.getString("EName");
		EPassword = result.getString("EPassword");
		EAddress = result.getString("EAddress");
		EEmail = result.getString("EEmail");
		enterprise.SetEnterpriseInfo(ENO,EName, EUser, EPassword, EAddress, EEmail);
		arrayList.add(enterprise);
		return arrayList;
	}
	
	public ArrayList<Enterprise> SeeAllEnterprise() throws SQLException, ClassNotFoundException
	{
		ArrayList<Enterprise> arrayList = new ArrayList<Enterprise>();
		
		String sql ="select * from Enterprise";
		Connection con = null;
		Statement stmt = null;
		ResultSet result = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		result = stmt.executeQuery(sql);
		while(result.next())
		{
			Enterprise enterprise = new Enterprise();
			ENO = result.getInt("ENO");
			EUser = result.getString("EUser");
			EName = result.getString("EName");
			EPassword = result.getString("EPassword");
			EAddress = result.getString("EAddress");
			EEmail = result.getString("EEmail");
			enterprise.SetEnterpriseInfo(ENO,EName, EUser, EPassword, EAddress, EEmail);
			arrayList.add(enterprise);
		}
		
		return arrayList;
	}
	
	
	public Enterprise FindEnterprise(String EUser) throws SQLException, ClassNotFoundException
	{
//		ArrayList<Enterprise> arrayList = null;
		Enterprise enterprise = new Enterprise();
		String sql ="select * from Enterprise where EUser ='"+EUser+"'";
		Connection con = null;
		Statement stmt = null;
		ResultSet result = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		result = stmt.executeQuery(sql);
		if(result.next())
		{	EUser = result.getString("EUser");
			EName = result.getString("EName");
			EPassword = result.getString("EPassword");
			EAddress = result.getString("EAddress");
			EEmail = result.getString("EEmail");
		}
		enterprise.SetEnterpriseInfo(ENO,EName, EUser, EPassword, EAddress, EEmail);
		return enterprise;
	}
	
	public boolean DeleteEnterprise(String EUser) throws ClassNotFoundException, SQLException
	{
		String sql ="delete from Enterprise where EUser='"+EUser+"'";
		Connection con = null;
		Statement stmt = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		if(stmt.executeUpdate(sql)==1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public boolean UpdateEnterprise(String EName,String EUser,String EPassword,String EAddress, String EEmail) throws ClassNotFoundException, SQLException
	{
		//can not update EUser
		String sql ="update Enterprise set EName='"+EName+"', EPassword='"+EPassword+"', EAddress='"+EAddress+"', EEmail='"+EEmail+"'where EUser='"+EUser+"'";
		System.out.println(sql);
		Connection con = null;
		Statement stmt = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		if(stmt.executeUpdate(sql)==1)
		{
			return true;//update successfully
		}
		else
		{
			return false;//update failed
		}
	}
}